const mongoose = Require('mongoose');
const Schema = mongoose.Schema;
const ContentSchema = new Schema({
    key: { type: String, required: true },
    name: { type: String, required: true },
    data: { type: Array, required: true },
    text: { type: String, required: true },
    disable: { type: Boolean, default: false }
}, { timestamps: true });

var formatDoc = function (doc) {
    var data = doc.toJSON({ getters: true });
    data.id = doc._id.toString();
    delete data.createdAt;
    delete data.updatedAt;
    delete data.disable;
    delete data._id;
    delete data.disable;
    return data;
};
var formatDocs = function (docs) {
    var _array = [];
    for (let i = 0; i < docs.length; i++) {
        const item = formatDoc(docs[i]);
        _array.push(item);
    };
    return _array;
};

var _Modal = function (options) {
    this.options = options;
    var username = this.options.username;
    var password = this.options.password;
    var ip = this.options.ip;
    var port = this.options.port;
    var DBName = this.options.DBName;
    var PorjectName = 'Modal';
    this.module = null;
    this.DBMessage = { message: 'HODE ON' };
    var DBURL = 'mongodb://';
    if (username && password) {
        DBURL = DBURL + username + ':' + password + '@';
    };
    DBURL = DBURL + ip + ':' + (port ? port : '80') + '/' + DBName;
    mongoose.connect(DBURL, { config: { autoIndex: false } });
    this.openEvent = function () { };
    mongoose.connection.on("error", (error) => {
        alert(error.message);
        this.openEvent('数据库连接错误');
        this.DBMessage = error.message;
    });
    mongoose.connection.on("open", () => {
        this.openEvent();
        this.DBMessage.message = false;
        this.module = mongoose.model(PorjectName, ContentSchema, PorjectName);
    });
};
_Modal.prototype.onOpen = function (event) {
    this.openEvent = event;
};

_Modal.prototype.creat = function (doc, cb) {
    if (this.DBMessage.message) {
        cb(this.DBMessage.message.message, null);
    } else {
        var data = new this.module(doc);
        // 将单行数据插入到数据库
        data.save(function (err, doc) {
            if (err) {
                cb(err.message, null);
            } else {
                cb(false, formatDoc(doc));
            };
        });
    };
};
_Modal.prototype.updata = function (id, _doc, cb) {
    if (this.DBMessage.message) {
        cb(this.DBMessage.message, null);
    } else {
        this.module.findById(id, function (err, doc) {
            doc.key = _doc.key;
            doc.name = _doc.name;
            doc.data = _doc.data;
            doc.text = _doc.text;
            doc.disable = false;
            doc.save(function (err, doc) {
                if (err) {
                    cb(err.message, null);
                } else {
                    cb(false, formatDoc(doc));
                };
            })
        });
    };
};

_Modal.prototype.getAll = function (cb) {
    if (this.DBMessage.message) {
        cb(this.DBMessage.message, null);
    } else {
        this.module.find({ disable: false }, function (err, docs) {
            if (err) {
                cb(error, null);
            } else {
                cb(false, formatDocs(docs));
            };
        });
    };
};

_Modal.prototype.delete = function (id, cb) {
    if (this.DBMessage.message) {
        cb(this.DBMessage.message, null);
    } else {
        // 删除单条记录
        this.module.findById(id, function (err, doc) {
            doc.disable = true;
            doc.save(function (err, doc) {
                if (err) {
                    cb(err.message, null);
                } else {
                    cb(false, formatDoc(doc));
                };
            })
        });
    };
};
module.exports = {
    modal: function (options) {
        return new _Modal(options);
    },
};